package com.sh.data.engine.domain.integration.job;

import com.sh.data.engine.domain.integration.api.http.service.ApiHttpService;
import com.sh.data.engine.domain.integration.api.webservice.service.ApiWsService;
import com.sh.data.engine.domain.integration.offline.model.enums.OfflineSyncExecuTypeEnum;
import com.sh.data.engine.domain.integration.offline.service.DataXService;
import com.sh.data.engine.job.core.context.XxlJobHelper;
import com.sh.data.engine.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
public class OfflineApiTask {

    @Autowired
    private ApiHttpService apiHttpService;

    @Autowired
    private ApiWsService apiWsService;

    @Autowired
    private DataXService dataXService;

    @XxlJob(value = "OfflineApiHttpHandler")
    public void runHttp() {
        final long jobId = XxlJobHelper.getJobId();

        Long apiId = apiHttpService.getApiIdByXxlId(jobId);

        if (Objects.isNull(apiId)) {
            XxlJobHelper.log("xxl-job id:{}对应的虎符中的任务不存在，执行结束....", jobId);
            return;
        }
        XxlJobHelper.log("任务拉起，开始异步执行");

        dataXService.executeApiHttp(apiId, OfflineSyncExecuTypeEnum.AUTOMIC.getStatus(), null);
    }

    @XxlJob(value = "OfflineApiWsHandler")
    public void runWs() {
        final long jobId = XxlJobHelper.getJobId();

        Long apiId = apiWsService.getApiIdByXxlId(jobId);

        if (Objects.isNull(apiId)) {
            XxlJobHelper.log("xxl-job id:{}对应的虎符中的任务不存在，执行结束....", jobId);
            return;
        }
        XxlJobHelper.log("任务拉起，开始异步执行");

        dataXService.executeApiWs(apiId, OfflineSyncExecuTypeEnum.AUTOMIC.getStatus(), null);
    }
}
